package com.rpframework.website.device.act.api;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rpframework.core.BaseAct;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.NumberUtils;
import com.rpframework.website.device.domain.Coupon;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.CouponService;

@Controller
@RequestMapping("/api/coupon")
public class CouponApiAct extends BaseAct{
	@Resource CouponService service;
	
	/**
	 * 查看当前用户所有可用优惠券
	 * */
	@RequestMapping(value="/list",produces = "application/json; charset=utf-8")
	public @ResponseBody String listByUserId(
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session
			){
		JsonObject json = new JsonObject();
		User user = getSessionUser(session);
		if(user == null){
			throw new APICodeException(-4, "您尚未登录！");
		}
		//获取当前时间 查询有效期内的券
		String nowTime = TagUtils.formatDate(System.currentTimeMillis()/1000);
		List<Coupon> list = service.searchList(user.getId(), nowTime);
		if(list.size()>0){
			json.addProperty("count", list.size());
			JsonArray array = new JsonArray();
			json.add("arrays", array);
			for(Coupon t : list){
				array.add(getJsonFormat(t));
			} 
		}else{
			json.addProperty("succ", false);
			json.addProperty("msg", "user"+"不存在或没有有效优惠券!");
			return (callback + "(" + json + ")");
		}
		json.add("remark",service.getJsonInfo());
		return  (callback + "(" + json + ")");
	}
	/**
	 * 优惠券信息
	 * */
	public JsonElement getJsonFormat(Coupon t) {
		JsonObject json = new JsonObject();
		json.addProperty("id", t.getId());
		json.addProperty("name", t.getName());
		json.addProperty("price", t.getPrice());
		json.addProperty("introduction", t.getIntroduction());
		json.addProperty("image", t.getImage());
		json.addProperty("startTime", t.getStartTime());
		json.addProperty("endTime", t.getEndTime());
		json.addProperty("userId", t.getUserId());
		json.addProperty("nowTime", TagUtils.formatDate(System.currentTimeMillis()/1000));
		return json;
	}
	/**
	 * 选择券查看当前详情
	 * */
	@RequestMapping("/detail")
	public @ResponseBody JsonElement detail(@RequestParam(value="id",required=false) Integer id){
		JsonObject json = new JsonObject();
		if(NumberUtils.isNotValid(id)){
			json.addProperty("succ", false);
			json.addProperty("msg", "id"+"为必填参数");
			return json;
		}
		Coupon t = service.searchById(id);
		if(t!=null){
			json.addProperty("succ", true);
			JsonArray array = new JsonArray();
			json.add("arrays", array);
			array.add(getJsonFormat(t));
		}
		return json;
	}
	/**
	 * 选择券使用
	 * */
	@RequestMapping("/used")
	public @ResponseBody JsonElement updateCoupon(@RequestParam(value="id",required=false) Integer id){
		JsonObject json = new JsonObject();
		if(NumberUtils.isNotValid(id)){
			json.addProperty("succ", false);
			json.addProperty("msg", "id"+"为必填参数");
			return json;
		}
		boolean flag = UserCoupon(id);
		if(flag){
			json.addProperty("succ",true);
			json.addProperty("msg","使用成功");
		}else{
			json.addProperty("succ",false);
			json.addProperty("msg","券不存在或已过期");
		}
		return json;
	}
	public boolean UserCoupon(Integer id){
		return service.userCoupon(id);
	}
	/**
	 * 某个优惠券被使用后 状态为禁用 但并未过期
	 * */
}
